package yingke.com.yingke.ui.left.hot;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import yingke.com.yingke.R;
import yingke.com.yingke.config.Constant;
import yingke.com.yingke.config.Creator;
import yingke.com.yingke.config.Live;
import yingke.com.yingke.ui.NEVideoPlayerActivity;
import yingke.com.yingke.ui.right.widget.CircleImageView;

/**
 * Created by Administrator on 2016/10/20 0020.
 */

public class HotListAdapter extends BaseAdapter{

    List<Live> mList;

    private Context mContext;
    public HotListAdapter(Context context){
        mContext =context;
    }
    public void setData(List<Live> list){
        this.mList =list;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mList!=null?mList.size():0;
    }

    @Override
    public Object getItem(int position) {
        return mList !=null?mList.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.hot_list_item,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Creator creator = mList.get(position).getCreator();
        String nick = creator.getNick();
        holder.tvName.setText(nick);
        holder.tvCount.setText(""+mList.get(position).getOnline_users());
        holder.tvLocation.setText(mList.get(position).getCity());
        String imgUrl = Constant.IMG_BASE_URL+creator.getPortrait();
        Glide.with(mContext).load(imgUrl).centerCrop().placeholder(R.drawable.default_nolive).into(holder.ivImg);
        Glide.with(mContext).load(imgUrl).centerCrop().placeholder(R.drawable.default_head).into(holder.civ);
        holder.ivImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NEVideoPlayerActivity.class);
                intent.putExtra("videoPath",mList.get(position).getStream_addr());
                intent.putExtra("media_type","livestream");
                intent.putExtra("decode_type","software");
                mContext.startActivity(intent);
            }
        });

        return  convertView;
    }

    static class ViewHolder {
        CircleImageView civ;
        TextView tvName, tvLocation,tvCount;
        ImageView ivImg;
        LinearLayout ll;
        public ViewHolder(View convertView) {
            civ = (CircleImageView) convertView.findViewById(R.id.civ_img_head);
            tvName = (TextView) convertView.findViewById(R.id.tv_hot_name);
            tvLocation = (TextView) convertView.findViewById(R.id.tv_left_location);
            tvCount = (TextView) convertView.findViewById(R.id.tv_left_count);
            ivImg = (ImageView) convertView.findViewById(R.id.iv_left_myimg);
        }
    }
}
